import {formatTime} from '../../../utils/util'
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataList: [{
        name: "全部",
        id: 0
      },
      {
        name: "待付款",
        id: '1'
      },
      {
        name: "待收货",
        id: '2'
      },
      {
        name: "已完成",
        id: '3'
      },
      {
        name: "退款/售后",
        id: '4'
      }
    ],
    listInfo:{
      page:1,
      size:4,
      status:''
    },
    indexs: 0,
    tabData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad(options) {
    if (options.index) {
      this.setData({
        indexs: parseFloat(options.index),
        ['listInfo.status']:options.index
      })
    }
  },
  handleChildData(e) {
    // console.log(e.detail)
    if(!e.detail){
      this.setData({
        ['listInfo.status']:"",
        ['listInfo.page']:1,
        indexs:0
      })
    }else{
      this.setData({
        ['listInfo.status']:e.detail,
        ['listInfo.page']:1
      })
    }
       this.getOrderlist()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      ['listInfo.page']:1
    })
    this.getOrderlist()
  },
 async getOrderlist(){
      // console.log(this.data.listInfo)
      let res = await getApp().api.getOrder(this.data.listInfo)
      // console.log(res)
      if(res.code!=200)return
      if(res.data.data.length>0){
        res.data.data.forEach(v=>{
          v.createTime = formatTime(new Date(v.createTime))
        })
      }
      if(this.data.listInfo.page==1){
        this.setData({
          tabData:res.data.data
        })
      }else{
        this.setData({
          tabData:this.data.tabData.concat(res.data.data)
        })
        if(res.data.data.length<=0){
          this.setData({
            ['listInfo.page']:this.data.listInfo.page-1
          })
        }
      }  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  //跳转
  todetail(e) {
    // console.log(e)
    wx.navigateTo({
      url: '/packageB/pages/orderDetail/index?id=' + e.currentTarget.id+'&status='+e.currentTarget.dataset.status,
    })
  },
  //取消订单
  async deleteOrder(e){
    Dialog.confirm({
      title: '提示',
      message: '确认取消订单？',
    })
      .then(async () => {
        // console.log(e.currentTarget.id)
         let res = await getApp().api.clearPay(e.currentTarget.id)
        // console.log(res)
         if(res.code!=200)return
         wx.showToast({
          title: '已取消！',
          icon:"success"
        })
         this.getOrderlist()
      })
      .catch(() => {
        // on cancel
      });
  },
  //删除订单
  async deteOrder(e){
    Dialog.confirm({
      title: '提示',
      message: '确认删除订单？',
    })
      .then(async () => {
        // console.log(e.currentTarget.id)
         let res = await getApp().api.deletePay(e.currentTarget.id)
        // console.log(res)
         if(res.code!=200)return
         wx.showToast({
           title: '删除成功！',
           icon:"success"
         })
         this.getOrderlist()
      })
      .catch(() => {
        // on cancel
      });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      ['listInfo.page']:this.data.listInfo.page+1
    })
    // console.log(1)
    this.throttle(this.getOrderlist(),500)
  },
  
 throttle(fn,wait){
  var timer = null;
  return function(){
      var context = this;
      var args = arguments;
      if(!timer){
          timer = setTimeout(function(){
              fn.apply(context,args);
              timer = null;
          },wait)
      }
  }
},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})